package com.beidouapp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beidouapp.cache.CacheConstants;
import com.beidouapp.cache.impl.RedisDefaultAppCacheHelper;
import com.beidouapp.dao.CityDao;
import com.beidouapp.domain.pojo.City;
import com.beidouapp.domain.pojo.Province;
import com.beidouapp.domain.pojo.Town;
import com.beidouapp.service.CityService;

@Service(value = "cityService")
public class CityServiceImpl implements CityService {
	
	@Autowired
	private CityDao cityDao;

	/** 日志记录器. */
	public static final Logger logger = LoggerFactory.getLogger(CityServiceImpl.class);
	
	public static final String[] MUNICIPALITY_CITY = new String[] { "110000","120000", "310000", "500000" };
	
	@Autowired
	private RedisDefaultAppCacheHelper jbossCacheProvider;
	

	@Override
	public List<Province> findProvinceList(Map<String, Object> paramters) {
		return cityDao.findProvinceList(paramters);
	}

	@Override
	public List<City> findCityList(Map<String, Object> paramters) {
		return cityDao.findCityList(paramters);
	}

	@Override
	public List<Town> findTownList(Map<String, Object> paramters) {
		return cityDao.findTownList(paramters);
	}

	@Override
	public boolean storeAllAddressList(Map<String, Object> cMap) {
		boolean flag = true;
		Map<String, Object> pMap = new HashMap<String, Object>();
		Map<String,String> proviceCodeMap =  new HashMap<String, String>();
		Map<String,String> cityCodeMap =  new HashMap<String, String>();
		Map<String,String> townCodeMap =  new HashMap<String, String>();
		try {
			List<Province> pList = findProvinceList(cMap);
			jbossCacheProvider.addCache(CacheConstants.ADDRESS_NAMESPACE, "province", pList);
			for (Province province : pList) {
				proviceCodeMap.put(province.getCode(), province.getName());
				pMap = new HashMap<String, Object>();
				pMap.put("p_code", province.getCode());
				List<City> cityList = findCityList(pMap);
				jbossCacheProvider.addCache(CacheConstants.ADDRESS_NAMESPACE, province.getCode(), cityList);
				for (City city : cityList) {
					cityCodeMap.put(city.getCode(), city.getName());
					pMap.put("c_code", city.getCode());
					List<Town> tList = findTownList(pMap);
					for(Town town: tList){
						townCodeMap.put(town.getCode(), town.getName());
					}
					jbossCacheProvider.addCache(CacheConstants.ADDRESS_NAMESPACE, city.getCode(), tList);
				}
			}
			
			List<City> currentList = findCityList(cMap);
			Map<String, Object> cityMap = new HashMap<String, Object>();
			Map<String, Province> proMap = new HashMap<String, Province>();
			if (pList != null && pList.size() != 0) {
				for (Province province : pList) {
					proMap.put(province.getCode(), province);
				}
				for (String str : MUNICIPALITY_CITY) {
					cityMap.put(str, proMap.get(str).getName());// 直辖市
					proviceCodeMap.put(str, proMap.get(str).getName());
				}
			}
			if (currentList != null && currentList.size() != 0) {
				for (City city : currentList) {
					cityMap.put(city.getCode(), city.getName());// 城市
					cityCodeMap.put(city.getCode(), city.getName());
				}
			}
			jbossCacheProvider.putAllMapValue(CacheConstants.ADDRESS_NAMESPACE, "pcode", proviceCodeMap);
			jbossCacheProvider.putAllMapValue(CacheConstants.ADDRESS_NAMESPACE, "ccode", cityCodeMap);
			jbossCacheProvider.putAllMapValue(CacheConstants.ADDRESS_NAMESPACE, "tcode", townCodeMap);
			jbossCacheProvider.addCache(CacheConstants.ADDRESS_NAMESPACE, CacheConstants.MUNICIPALITY_CITY_KEY, cityMap);// 存储直辖市和城市一级
		} catch (Exception e) {
			flag = false;
			logger.error("获取省市区列表异常！", e);
		}
		return flag;
	}

}
